/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.Random;

/**
 *
 * @author mehlicious, karsonbraaten
 */
public class Model {
    public String story;
    
    public Data data;
    public StopWatch watch;
    
    private int MAX_TRIALS = 24;
    private int numTrials;
    
    private String textName = "read.txt";
    private String outFile = "Testout";
    
    public boolean done;
    
    public LinkedList<Phrase> phrases;
    public LinkedList<Phrase> compPhrases;
    
    private Random r = new Random();
    
    // Footprint Scroll bar search phrases the participant will find
    private String p1 = "In other studies you go as far as others have gone before you, and there is nothing more to know; but in a scientific pursuit there is continual food for discovery and wonder. A mind of moderate capacity, which closely pursues one study, must infallibly arrive at great proficiency in that study";
    private String p2 = "\"I did confess; but I confessed a lie. I confessed, that I might obtain absolution; but now that falsehood lies heavier at my heart than all my other sins. The God of heaven forgive me! Ever since I was condemned, my confessor has besieged me; he threatened and menaced, until I almost began to think that I was the monster that he said I was.";
    private String p3 = "This was a new sight to me; and I examined the structure with great curiosity. Finding the door open, I entered. An old man sat in it, near a fire, over which he was preparing his breakfast. He turned on hearing a noise; and, perceiving me, shrieked loudly, and, quitting the hut, ran across the fields with a speed of which his debilitated form hardly appeared capable.";
    private String p4 = "Presently two countrymen passed by; but, pausing near the cottage, they entered into conversation, using violent gesticulations; but I did not understand what they said, as they spoke the language of the country, which differed from that of my protectors.";
    private String p5 = "I threw myself into the carriage that was to convey me away, hardly knowing whither I was going, and careless of what was passing around. I remembered only, and it was with a bitter anguish that I reflected on it, to order that my chemical instruments should be packed to go with me.";
    private String p6 = "I have endured toil and misery: I left Switzerland with you; I crept along the shores of the Rhine, among its willow islands, and over the summits of its hills. I have dwelt many months in the heaths of England, and among the deserts of Scotland.";
    private String p7 = "While this strange dialogue continued, I perceived the crowd rapidly increase. Their faces expressed a mixture of curiosity and anger, which annoyed, and in some degree alarmed me.";
    private String p8 = "My father yielded at length to my desire to avoid society, and strove by various arguments to banish my despair. Sometimes he thought that I felt deeply the degradation of being obliged to answer a charge of murder, and he endeavoured to prove to me the futility of pride.";
    private String p9 = "Thus Elizabeth endeavoured to divert her thoughts and mine from all reflection upon melancholy subjects. But her temper was fluctuating; joy for a few instants shone in her eyes, but it continually gave place to distraction and reverie.";
    private String p10 = "\"I do not doubt that he hovers near the spot which I inhabit; and if he has indeed taken refuge in the Alps, he may be hunted like the chamois, and destroyed as a beast of prey. But I perceive your thoughts: you do not credit my narrative, and do not intend to pursue my enemy with the punishment which is his desert.\"";
    private String p11 = "Thus has a week passed away, while I have listened to the strangest tale that ever imagination formed. My thoughts, and every feeling of my soul, have been drunk up by the interest for my guest, which this tale, and his own elevated and gentle manners, have created. I wish to soothe him";
    private String p12 = "I was at first touched by the expressions of his misery; yet, when I called to mind what Frankenstein had said of his powers of eloquence and persuasion, and when I again cast my eyes on the lifeless form of my friend, indignation was rekindled within me.";
    
    public Model () {
        data = new Data();
        watch = new StopWatch();
        story = "";
        numTrials = 0;       
        done = false;
        
        // Search phrases the participant will find
        phrases = new LinkedList<Phrase>();
        
        // Order:
        // p1 … p6, p2, p4, p1, p5, p2, p4 … p12, p3, p11, p3, p11, p3,  p11
        phrases.add(new Phrase(p1));
        phrases.add(new Phrase(p2));
        phrases.add(new Phrase(p3));
        phrases.add(new Phrase(p4));
        phrases.add(new Phrase(p5));
        phrases.add(new Phrase(p6));
        
        phrases.add(new Phrase(p2));
        phrases.add(new Phrase(p4));
        phrases.add(new Phrase(p1));
        phrases.add(new Phrase(p5));
        phrases.add(new Phrase(p2));
        phrases.add(new Phrase(p4));
        
        phrases.add(new Phrase(p7));
        phrases.add(new Phrase(p8));
        phrases.add(new Phrase(p9));
        phrases.add(new Phrase(p10));
        phrases.add(new Phrase(p11));
        phrases.add(new Phrase(p12));
        
        phrases.add(new Phrase(p3));
        phrases.add(new Phrase(p11));
        phrases.add(new Phrase(p3));
        phrases.add(new Phrase(p11));
        phrases.add(new Phrase(p3));
        phrases.add(new Phrase(p11));
        
        // Read the text file
        try {
            BufferedReader read = new BufferedReader(new FileReader(textName));

            String line = null;
            
            while((line = read.readLine()) != null){ story += line + "\n"; }
            
            read.close();
            
        } catch (IOException ex) {
            System.out.print(ex.toString());
        }
    }
    
    public void writeDataToFile(){
        try {
            PrintWriter write = new PrintWriter(new FileWriter(outFile + r.nextInt(100) + ".txt"));
            write.write(data.getTabDelimitedData().toString());
            write.close();
        } catch (IOException e){
            System.out.println(e.toString());
        }
    }
    
    public boolean found(String s){
        
        if(numTrials == MAX_TRIALS - 1) { done = true; }
        addData();
        
        numTrials++;
        return true;
    }
    
    public void startTime(){ watch.start(); }
    
    public void addData(){
        data.addData(watch.getElapsedTimeSecs());
        System.out.println(data.toString());
    }
    
    public void addError(){
        data.addError();
        System.out.println(data.toString());
    }
    
    public void stopTime() { watch.stop(); }

    public Phrase getPhrase(){
        if(!phrases.isEmpty()){
            return phrases.removeFirst(); 
        }
        else{
            return null;
        }
    }
}
